package com.ewhome.apartment.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.ewhome.apartment.R;
import com.ewhome.apartment.di.component.DaggerLoginActivityComponent;
import com.ewhome.apartment.di.module.LoginActivityModule;
import com.ewhome.apartment.mvp.contract.LoginActivityContract;
import com.ewhome.apartment.mvp.presenter.LoginActivityPresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.tbruyelle.rxpermissions2.RxPermissions;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class LoginActivityActivity extends BaseActivity<LoginActivityPresenter> implements LoginActivityContract.View {


    @BindView(R.id.username)
    EditText etusername;
    @BindView(R.id.password)
    EditText etpassword;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @Inject
    RxPermissions mRxPermissions;
    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerLoginActivityComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .loginActivityModule(new LoginActivityModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_login; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(Bundle savedInstanceState) {
//        mPresenter.requestPhoneState();
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @OnClick(R.id.btn_login)
    public void onClick(View v){
        switch (v.getId()){
            case R.id.btn_login:
                login();
                break;
        }
    }

    public void login(){
        String username=etusername.getText().toString();
        String password=etpassword.getText().toString();
        if (TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){
            ArmsUtils.snackbarText("用户名或者密码不能为空");
            return;
        }
        mPresenter.testLogin(username,password);
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    //申请权限
    public RxPermissions getRxPermissions(){
        return mRxPermissions;
    }

}
